home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.1 / Examples1 / PCMCIA / PCMCIA.Software < prev    next >
Encoding:
Text File  |  1999-10-27  |  18.3 KB  |  417 lines

  1.  
  2. Preparing Software for Credit-Card Distribution
  3. by Darren Greenwald
  4.  
  5. (c) Copyright 1991-1999 Amiga, Inc.  All Rights Reserved
  6.  
  7.  
  8.    ************************************************************************
  9.    *                                      *
  10.    *                DISCLAIMER                  *
  11.    *                                      *
  12.    *   THIS SOFTWARE AND INFORMATION IS PROVIDED "AS IS".          *
  13.    *   NO REPRESENTATIONS OR WARRANTIES ARE MADE WITH RESPECT TO THE      *
  14.    *   ACCURACY, RELIABILITY, PERFORMANCE, CURRENTNESS, OR OPERATION      *
  15.    *   OF THIS SOFTWARE AND INFORMATION, AND ALL USE IS AT YOUR OWN RISK. *
  16.    *   NEITHER AMIGA NOR THE AUTHORS ASSUME ANY RESPONSIBILITY OR      *
  17.    *   LIABILITY WHATSOEVER WITH RESPECT TO YOUR USE OF THIS SOFTWARE      *
  18.    *   AND INFORMATION.                              *
  19.    *                                      *
  20.    ************************************************************************
  21.  
  22. About This Document
  23.  
  24. The purpose of this document is to explain the issues associated
  25. with developing Amiga software for distribution in ROM card format.
  26. Before reading this document, you may wish to first read through
  27. the ``PCMCIA PC Card Standard - Release 2.0''.  In particular read 
  28. through the section titled ``Card Metaformat.''  See also the
  29. Amiga example code, autodoc, and include files for the card device.
  30.  
  31. The PCMCIA Card Standard Specifications may be ordered directly
  32. from PCMCIA.
  33.  
  34.     PCMCIA Resource Ref Book - approximately $7.50
  35.     PCMCIA Specifications - approximately $250.00 
  36.  
  37.     PCMCIA - Phone 408-720-0107  (California, USA)
  38.  
  39. This is the order number.  Call to get exact pricing and then you can
  40. FAX an order direct to PCMCIA.  The prices above are for non-members.
  41.  
  42.  
  43. Background
  44.  
  45. Many developers, especially those with entertainment products will
  46. be interested in distributing their software in ROM format. The
  47. credit-card package is ideal. There are two ways that programs can
  48. be organized on ROM cards:
  49.  
  50.     1) ``Execute-In-Place'' format
  51. and
  52.     2) ``Disk-Like'' (or File-Formatted) format.
  53.  
  54. We do not anticipate that the first Amiga implementation will support
  55. the DOS XIP protocol which is clearly MS-DOS oriented. This protocol
  56. is complex, and there is no real benefit because Amiga computers cannot
  57. run XIP applications intended for MS-DOS machines.
  58.  
  59. The term ``Execute-In-Place'' means that program code can run directly
  60. from the ROM card, i.e., it is not downloaded into the Amiga's RAM.
  61. Amiga execute-in-place software will run in an environment similar to
  62. boot block games; that is, they will run before DOS has been initialized.
  63. Such software may not initialize DOS, as DOS requires a suitable device
  64. and disk-like media to boot off. 
  65.  
  66. As an alternative to the Amiga execute-in-place environment, software
  67. can be distributed using a disk-like format. This provides less
  68. copy-protection than execute-in-place cards, and requires use of
  69. available RAM to load such software. However plenty of existing software
  70. can be easily placed on a ROM using the disk-like format. Software
  71. distributed in this format would also be usable on a diskless Amiga.
  72. If a formatted and bootable card is not inserted when the machine boots,
  73. the user is prompted to insert a disk (or card in this case) by the
  74. Amiga strap module.
  75.  
  76. The advantage of this format is that it provides an environment to
  77. initialize DOS (though many boot block games do not do so) should you
  78. wish to save high scores or other data to another Static RAM card or
  79. floppy disk using standard DOS calls.  An other advantage is that your
  80. code does not have to be rommable.  Any program that can be started from
  81. a bootable floppy's startup-sequence should work from a disk-format
  82. PCMCIA card with little or no modification.
  83.  
  84. RAM PCMCIA cards card be formatted as arbitrary-size disks using the
  85. PrepCard utility which comes with the Workbench of Amigas with a
  86. PCMCIA slot.  They can be made bootable by installing them using
  87. the special Install command shipped with the machine which will
  88. install trackdisk or carddisk.  The contents of a floppy can be
  89. copied to the formatted disk-format PCMCIA RAM card with a simple
  90. COPY DF0: CC0: ALL.  If you have custom boot code, you should be able
  91. to install it by using the carddisk.device exec device directly.
  92.  
  93.  
  94. General PCMCIA Issues:
  95.  
  96. Important things to be aware of:
  97.  
  98. * Execute-in-place code cannot use, and does not need to use custom
  99.   trackdisk code to load itself from floppy disk.
  100.  
  101. * Self-modifying code (which has always been discouraged) will break
  102.   if running out of ROM. Such code must be moved to RAM for execution.
  103.   You already know that self modifying code tends to break on CPUs with
  104.   instruction/data caches turned on.
  105.  
  106. * Existing executables could be LoadSeg()ed into credit-card memory to
  107.   resolve references.  We provide a "loadc" tool for ROMTAG'd executables
  108.   which will loadseg the executable into a PCMCIA RAM card, automatically
  109.   surrounding it with the necessary tuples for AmigaXIP.
  110.   Nevertheless, such programs still might not run out of ROM if the CODE
  111.   tries to write to any DATA or BSS sections.  (NOTE - see the tool "loadc")
  112.  
  113. * Special data such as tuples can be placed into the card image by using
  114.   a debugger.
  115.  
  116. * File format cards appear just like diskettes for the system.  They can
  117.   be formatted, and standard AmigaDOS commands can be used on them.
  118.  
  119. * Executable code for execute-in-place cards usually cannot be written
  120.   using only standard compilers and support files because included
  121.   startup code for example may read and write to global variables in
  122.   the executable image, which would not be writable, of course, when
  123.   in ROM.
  124.  
  125. * Execute-in-place code can be tested on a static RAM card which is write
  126.   protected once the code has been loaded into it.
  127.  
  128. * You should check with ROM vendors to find out what media they can accept
  129.   an image on for production purposes.
  130.  
  131.  
  132. Execute-In-Place Format
  133.  
  134. Amiga execute-in-place software will require that a minimal Card
  135. Information Structure be present. A CISTPL_DEVICE tuple (as required by
  136. the standard) must be present, and an additional tuple which will identify
  137. the card as an Amiga execute-in-place card.
  138.  
  139.  
  140. Attribute Memory Characteristic Problems
  141.  
  142. There is a problem which has not been made clear in the PCMCIA
  143. specification which you will need to be aware of. Four out of four
  144. of the Static RAM cards we have now have different attribute memory
  145. characteristics. This will become an important issue once you are ready
  146. to go to a Masked ROM vendor for duplication. The attribute memory
  147. characteristics of the card on which you develop the master will affect
  148. how the Card Information Structure can, and must be laid out. You will
  149. need to make sure that the Masked ROM vendor you deal with can fully
  150. duplicate the card, including attribute memory.
  151.  
  152. An example may help to clarify this point. You might develop your master
  153. on a write-protected Static RAM card, or EEPROM card which has a few bytes
  154. of writeable attribute memory. You might lay out the Card Information
  155. Structure like so: 
  156.  
  157.     Attribute memory
  158.     CISTPL_DEVICE,            /* tuple    */
  159.     0x00,                    /* skip    */
  160.     0x02,                /* link        */
  161.     0x00,                    /* skip    */
  162.     DTYPE_ROM|DPSEED_250ns,        /* type/speed    */
  163.     0x00,                    /* skip    */
  164.     ((1<<3)|4,            /* units/size    */
  165.     0x00,                    /* skip */
  166.     CISTPL_END,            /* tuple    */
  167.     0x00,                    /* skip    */
  168.     CISTPL_END,            /* link        */
  169.  
  170.     Common Memory
  171.     CISTPL_LINKTARGET,        /* tuple    */
  172.     0x03,                /* link        */
  173.     'C','I','S',            /* body        */
  174.  
  175.     CISTPL_?????,            /* etc.        */
  176.  
  177. There is an implied link from attribute memory to common memory, so
  178. the tuple chain would be continued in common memory following the
  179. CISTPL_LINKTARGET tuple stored at 0x00000000 in common memory.
  180. However, once you take this to the Masked ROM vendor, this Card
  181. Information Structure may not work if the vendor's ROMS have overlapped
  182. attribute/common memory. An example of this would be an Intel Flash ROM.
  183.  
  184. Some of the Static RAM cards do not have any writeable attribute memory,
  185. and read $FF when read. This is technically not correct because the
  186. CISTPL_DEVICE should appear as the first tuple in attribute memory.
  187. However, because we do have examples of such cards, we will allow the
  188. CISTPL_DEVICE tuple to be stored in common memory after a CISTPL_LINKTARGET
  189. tuple.  You may use any of the level 1 basic compatibility tuples on the
  190. card to layout the Card Information Structure. With some care it is possible
  191. to store the CIS in very little space. Make use of a CISTPL_END tuple link
  192. to terminate a tuple chain, and the implied link from attribute to common
  193. memory to continue a chain.
  194.  
  195. You may choose to store manufacturer information on the card using the
  196. CISTPL_VERS_1, or CISTPL_VERS_2 tuples. This information is not required,
  197. though it may be important for you to place identification information 
  198. on a Masked ROM.
  199.  
  200.  
  201.  
  202.  
  203. Going from RAM to ROM (or FLASHROM)
  204.  
  205. CATS would eventually like to provide a utility which will read selected
  206. tuples from any type of RAM card, plus the binary contents of the common
  207. memory of the RAM card, to create a flat binary file intended for an
  208. overlapped-attribute Flashrom or ROM card.  Link tuples would be generated
  209. by the utility.
  210.  
  211. CATS plans to support the following tuples in this utility,
  212. but we do not plan to support multiple instances of them:
  213.  
  214. CISTPL_DEVICE (describes device type, speed, size)
  215.  
  216. CISTPL_FORMAT (indicates card contains data, where the data is, size of
  217. partition, partition type [e.g., DISK like], error detection method, etc.)
  218.  
  219. CISTPL_GEOMETRY (only meaningful for DISK like data; contains sec/trk,
  220. trk/cyls, total cylinders).  Note that there can be more than one
  221. CISTPL_FORMAT tuple, though our device driver does NOT look for more than
  222. one.  Also more than one CISTPL_GEOMETRY (e.g., 1 per CISTPL_FORMAT tuple)
  223.  
  224. CISTPL_VERS1 (product information - note we've never seen one on any card
  225. except for the modem).
  226.  
  227. CISTPL_VERS2 (indicates degree of compliance with the spec, and info
  228. about who wrote the tuples - Good idea to duplicate this one, though
  229. the built-in device driver does not require it).
  230.  
  231. CISTPL_AMIGAXIP (this is not a standard tuple; it is one of the ones
  232. reserved for manufacturer specific use, and should not appear on any
  233. cards but our own Amiga specific cards).
  234.  
  235.  
  236.  
  237. The CISTPL_AMIGAXIP Tuple
  238.  
  239. In order to identify the card as an Amiga execute-in-place card, you must
  240. also place a CISTPL_AMIGAXIP tuple in the CIS. This is not a standard
  241. tuple, and should not appear by default on any cards.
  242.  
  243. Tuples can be expressed as structures. The structure of a CISTPL_AMIGAXIP
  244. tuple looks like: 
  245.  
  246. struct TP_AmigaXIP {
  247.     UBYTE TPL_CODE;        /* 0x91 (hex $91)        */
  248.     UBYTE TPL_LINK;        /* must be $6            */
  249.     UBYTE     TP_XIPLOC[4];    /* offset to ROM-TAG        */
  250.     UBYTE     TP_XIPFLAGS;    /* boot flags            */
  251.     UBYTE     TP_XIPRESRV;    /* must be 0            */
  252. };
  253.  
  254. The TPL_CODE must be equal to 0x91 ($91 hex), and the TPL_LINK field must
  255. be equal to 0x6 ($6 hex). The TP_XIPLOC field is a 4 byte offset into
  256. common memory, stored in little-endian order (which is consistent with
  257. the standard for storing 32 bit offsets in tuples).
  258.  
  259. This offset (when added to the base address of common memory) must
  260. point to a valid ROM-TAG (struct Resident). If all the above is true,
  261. your execute-in-place code will be called at strap time by Exec's
  262. InitResident() function.
  263.  
  264. Bit 0 of the TP_XIPFLAGS field (if set) indicates that the machine
  265. should reboot if the card is inserted after DOS has been started,
  266. thereby allowing the execute-in-place code to run as soon as the system
  267. finishes rebooting. This feature may not be implemented on all machines.
  268. For example, on a machine which is more clearly a computer than an
  269. entertainment system, we will likely not implement this feature. 
  270. Because a ROM-TAG is used, it is implied that such software does not
  271. need to use PC-Relative code. Absolute references are allowed, and
  272. encouraged if it will decrease the time necessary to port software to
  273. a ROMABLE form. Amiga machines which have a credit-card slot and support
  274. this Amiga execute-in-place method will map the start of credit-card
  275. common memory to $600000 through $9FFFFF inclusive. This is within the
  276. address space accessible by the 68000 CPU, and uses the upper 4 megabytes
  277. of address space for FAST ram expansion. Attribute memory will appear
  278. at $A00000 through $A1FFFF.
  279.  
  280.  
  281.  
  282. Final Notes for Execute-in-Place Software
  283.  
  284. As indicated above, execute-in-place software operates in an environment
  285. similar to boot block games. The system will poll for newly inserted
  286. execute-in-place cards at the same time that it polls for other devices
  287. (e.g., hard disks and floppies) to boot from. Execute-in-place cards
  288. always take priority, so even systems with hard disks can run execute-
  289. in-place cards.  Some software, including many games, will never return
  290. control to the system. If you do return from InitResident(), you should
  291. return a non-successful return code in D0 (NULL) indicating that you are
  292. done with the card and that it may be removed by the user. You must leave
  293. the system in a ``good'' state. The system will try to pick up where it
  294. left off, polling for something to boot off. You should definitely test
  295. your exit code. If you are unable to exit cleanly, it is better not to
  296. do so. Games which completely take over the system would be better off
  297. performing a reboot rather than exiting.
  298.  
  299. Because a ROM-TAG is used, you should make use of InitResident()'s
  300. ability to allocate memory for you, and initialize that memory. This is
  301. precisely what we use for nearly all Operating System ROM modules;
  302. it works well. 
  303.  
  304.  
  305. Disk-Like or File Format for ROM Cards
  306.  
  307. In addition to a CISTPL_DEVICE tuple, such a card must have a valid
  308. CISTPL_FORMAT tuple which indicates that the card is formatted like
  309. a disk. The use of CISTPL_GEOMETRY tuple is optional.  If the software
  310. is being distributed on a Masked ROM, the CISTPL_DEVICE tuple should
  311. indicate this, and there is no reason to use Checksum, or CRC error
  312. detection. The software will load faster without error detection,
  313. and you save some valuable ROM space.
  314.  
  315. PCMCIA-slot equipped Amigas contain a basic device driver in ROM which
  316. supports autobooting off formatted cards if the card has been formatted
  317. by the Amiga FileSystem, and has valid boot blocks (like an installed
  318. floppy disk). The master can be easily created on a Static RAM card, and
  319. write-protected for testing and duplication.  Such cards can be easily
  320. formatted with the PrepCard utility, and made bootable with a special
  321. Install command which supports CC0: (the credit card drive).
  322.  
  323.  
  324.  
  325. Mixing Execute-in-Place and File Formatted Cards
  326.  
  327. While there will be no built-in support for mixing execute-in-place
  328. and file formatted cards, we anticipate some developers will wish to
  329. do so. It is possible to use a small portion of a Masked ROM as a 
  330. bootable Amiga disk, and have a small program(s) which calls
  331. execute-in-place code stored elsewhere on the card.  The only real
  332. problem with doing this is the machine will likely crash if the card
  333. is removed. Because the card looks like a disk, the user might reasonably
  334. assume that HOT-REMOVAL is acceptable, as would be the case for a floppy
  335. disk. We provide an approved-of ``cheat'' below so that the machine
  336. will at least reboot instead of crashing if such a card is removed. In
  337. either case, you will need to tell the user the consequences of removing
  338. the card while the machine is on. In a multitasking environment the user
  339. may want to run some other important software; crashing and rebooting
  340. are rarely appreciated by the user.
  341.  
  342. Hidden executable code on a file-formatted card (ie. code stored outside
  343. of the bootable disk image) should not be described by an AMIGAXIP tuple
  344. since future implementations of the OS software might find and initialize
  345. multiple tuples.
  346.  
  347. Format your PCMCIA as a small bootable disk (less cylinders than the
  348. size of the card).  Modify loadseg.asm to load your run-in-place code
  349. into the PCMCIA card past where the disk ends (this will vary depending
  350. on the checksum method chosen for the disk but should be covered by the
  351. partition size in the PCMCIA FORMAT tuple of your formatted PCMCIA
  352. disk-format card -  see the PCMCIA Card Standard).
  353.  
  354. Write a small program to be called from your startup-sequence in the disk
  355. part of the card - this small program will find and call your hidden
  356. execute-in-place code.
  357.  
  358. Because this hidden-execute-in-place method boots as a disk,
  359. the system will think the PCMCIA card is removable (i.e. the system
  360. will not reset if the card is removed).  If you are running code or
  361. using data in the card when this happens, the system will likely crash.
  362. It is better to make sure the system will reboot in such cases.
  363. Therefore, your startup-sequence program should do the following before
  364. calling your hidden execute-in-place code to make sure that the system
  365. will reset upon removal of the card (see also cardres.doc):
  366.  
  367. Forcing Reset-on-removal of a disk-formatted card:
  368.  
  369. - OpenResource the card.resource
  370. - Try to own the card with OwnCard()
  371. - If if fails (as it normally would if the card is disk format and
  372.   is inserted), then
  373. - Use the CardHandle pointer returned by OwnCard() to call
  374.   CardResetRemove() to enable reset on removal.  If this returns
  375.   a successful return, then it is safe to call your hidden
  376.   execute-in-place code on the card.
  377.  
  378. Good programming practice suggests disabling reset-on-removal
  379. when your execute-in-place code or data use is completed.
  380.  
  381.  
  382. See the Compiler Issues section for more information on how to
  383. write/call rommable code.
  384.  
  385.  
  386. Compiler Issues:
  387.  
  388. Generally, rommable code is written in assembler, but you may be able
  389. to write rommable C code using your compiler's method for writing pure
  390. (reentrant) code.  In addition, some compilers may directly support
  391. creation of rommable code.
  392.  
  393. Consult your compiler's documentation regarding rommable and reentrant code.
  394. Such mechanisms often use base-relative data references and special startup
  395. code that clones your global data segment into RAM so that you can write to
  396. your global variables.  You may also need to use #pragmas for system calls
  397. rather than using amiga.lib stubs.
  398.  
  399. Calling the code:
  400.  
  401. If you plan to start your code from an AmigaXIP ROMTAG, you will probably
  402. need to modify the compiler-provided startup code to not open/use DOS, and
  403. to not Wait or Reply a WBStartup message.
  404.  
  405. If you call your code as a subroutine from a disk-format PCMCIA card
  406. startup-sequence program, you will need to call it with d0 and a0 set
  407. up appropriately for CLI program entry, i.e.:
  408.  
  409. *           d0  dosCmdLen
  410. *           a0  dosCmdBuf
  411.  
  412. If you instead CreateProc or CreateNewProc a hidden execute-in-place
  413. program, you will either have to modify its startup code to not Wait
  414. for or Reply a WBStartup message, or have your small boot program send
  415. a fake WBStartup message to the Process MsgPort of your CreateProc'd
  416. hidden code to wake it up.
  417.